/**
 * 
 */
package instant.mail;

import java.util.Properties;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

/**
 * @author qiangli
 * 
 */
public final class Preferences {
	public static final String ETALK = "etalk";

	public static final String SETUP_RUN = "etalk.setup.run";
	
	//contacts sorting
	private static final String SORT_DIR = "etalk.sort.dir";
	private static final String SORT_BY = "etalk.sort.by";
	
	//mode
	
	private static final String ETALK_MODE = "etalk.mode";
	
	//mail
	public static final String EMAIL = "mail.smtp.from";
	public static final String NICKNAME = "x.mail.smtp.nickname";
	public static final String STARTTLS_ENABLE = "mail.smtp.starttls.enable";
	public static final String USERNAME = "mail.smtp.user";
	public static final String PASSWORD = "x.mail.smtp.password"; //
	public static final String HOST = "mail.smtp.host";
	public static final String PORT = "mail.smtp.port";
	public static final String SIGNATURE = "x.mail.smtp.signature";
	public static final String SIGNATURE_LOCATION = "x.mail.smtp.signature.location";
	public static final String SIGNATURE_TIME = "x.mail.smtp.signature.time";
	public static final String SIGNATURE_PHOTO = "x.mail.smtp.signature.photo";
	public static final String REPLYTO = "x.mail.smtp.replyto";

	//setting - recorder
	private static final String ONEBUTTON_TOGGLE = "compose.onebutton.toggle";
	private static final String SENDCLOSE_TOGGLE = "compose.sendclose.toggle";
	private static final String AUTOSTART_TOGGLE = "compose.autostart.toggle";
	
	//message
	private static final String SHOWHEADER_TOGGLE = "compose.showheader.toggle";
	private static final String SHOWHEADER_TO_TOGGLE = "compose.showheader.to.toggle";
	private static final String SHOWHEADER_SUBJECT_TOGGLE = "compose.showheader.subject.toggle";
	private static final String SHOWBODY_TOGGLE = "compose.showbody.toggle";
	
	//bg image
	public static final String BG_SELECT = "compose_bg_select";
	//must match values compose_bg_select_values in arrays.xml
	public static final int BG_SELECT_PHOTO = 0;
	public static final int BG_SELECT_PICK = 1;
	public static final int BG_SELECT_SAMPLE_ANDROID = 2;
	public static final int BG_SELECT_SAMPLE_BLUESKEY = 3;
	public static final int BG_SELECT_SAMPLE_GOODMORNING = 4;
	public static final int BG_SELECT_NO_BACKGROUND = 5;
	public static final int BG_SELECT_GONE_WILD = 6;
	
	//
	private static final String BG_PICK_PATH = "compose_bg_pick_path";

	//advanced 
	private static final String AUDIO_FORMAT = "compose.audio.format";
	private static final String AUDIO_SAMPLE_RATE = "compose.audio.samplerate";
	private static final String AUDIO_MAXSIZE = "compose.audio.maxsize"; //in KB
	private static final String ETALK_FOLDER = "etalk.folder";

	//greeting
	private static final String GREETING = "greeting";
	private static final String NOTE_BODY = "greeting.note.body";
	private static final String NOTE_SUBJECT = "greeting.note.subject";


	/** 
	 */
	private Preferences() {
	}

	public static int getSortBy(Context context, int defValue) {
		try {
			final SharedPreferences pref = context.getSharedPreferences(
					ETALK, Activity.MODE_PRIVATE);
			return pref.getInt(SORT_BY, defValue);
		} catch (Exception e) {
		}
		return defValue;
	}

	public static int getSortDir(Context context, int defValue) {
		try {
			final SharedPreferences pref = context.getSharedPreferences(
					ETALK, Activity.MODE_PRIVATE);
			return pref.getInt(SORT_DIR, defValue);
		} catch (Exception e) {
		}
		return defValue;
	}
	
	public static void putSortBy(Context context, int sortBy) {
		final SharedPreferences pref = context.getSharedPreferences(ETALK,
				Activity.MODE_PRIVATE);
		pref.edit().putInt(SORT_BY, sortBy).commit();
	}
	
	public static void putSortDir(Context context, int sortDir) {
		final SharedPreferences pref = context.getSharedPreferences(ETALK,
				Activity.MODE_PRIVATE);
		pref.edit().putInt(SORT_DIR, sortDir).commit();
	}

	public static int getEtalkMode(Context context, int defval) {
		//http://code.google.com/p/android/issues/detail?id=2096
		try {
			final SharedPreferences pref = PreferenceManager
			.getDefaultSharedPreferences(context);
			return Integer.valueOf(pref.getString(ETALK_MODE, defval+""));
		} catch (Exception e) {
		}
		return defval;
	}

	public static void putEtalkMode(Context context, int mode) {
		final SharedPreferences pref = PreferenceManager
		.getDefaultSharedPreferences(context);
		pref.edit().putString(ETALK_MODE, mode+"").commit();
	}

	private static final Properties props = new Properties();
	
	public static final String GMAIL_DOMAIN = "@gmail.com";
	public static final String DEFAULT_HOST = "smtp.gmail.com";
	public static final String DEFAULT_PORT = "25";

	private static final int AUDIO_SAMPLE_RATE_DEFAULT = 8000;

	public static Properties getPreferences(Context context) {
		final SharedPreferences p = PreferenceManager
		.getDefaultSharedPreferences(context);
		
		props.put(EMAIL, p.getString(EMAIL, ""));
		props.put(USERNAME, p.getString(USERNAME, ""));
		props.put(PASSWORD, p.getString(PASSWORD, ""));
		props.put(NICKNAME, p.getString(NICKNAME, ""));
		props.put(REPLYTO, p.getString(REPLYTO, ""));
		
		props.put(HOST, p.getString(HOST, ""));
		props.put(PORT, p.getString(PORT, ""));
		props.put(STARTTLS_ENABLE, Boolean.toString(p.getBoolean(STARTTLS_ENABLE, true)));
		
		props.put(SIGNATURE, p.getString(SIGNATURE, ""));
		
		return props;
	}
	
	/**
	 * Check if required fields email, username, password not null.
	 * @param context Activity
	 * @return true if required fields not null
	 */
	public static boolean isAccountConfigured(Context context) {
		//
		final SharedPreferences p = PreferenceManager
		.getDefaultSharedPreferences(context);
		if (TextUtils.isEmpty(p.getString(EMAIL, null))) {
			return false;
		}
		if (TextUtils.isEmpty(p.getString(USERNAME, null))) {
			return false;
		}
		if (TextUtils.isEmpty(p.getString(PASSWORD, null))) {
			return false;
		}
		return true;
	}

	public static String getSignature(Context context) {
		final SharedPreferences p = PreferenceManager
		.getDefaultSharedPreferences(context);
		return p.getString(SIGNATURE, "");
	}
	
	public static String getReturnEmail(Context context) {
		final SharedPreferences p = PreferenceManager
		.getDefaultSharedPreferences(context);
		String a = p.getString(REPLYTO, null);
		if (TextUtils.isEmpty(a)) {
			a = p.getString(EMAIL, null);
		}
		return a;
	}
	
	public static boolean isOneButton(Context context) {
		try {
			final SharedPreferences pref = PreferenceManager
			.getDefaultSharedPreferences(context);
			return pref.getBoolean(ONEBUTTON_TOGGLE, false);
		} catch (Exception e) {
		}
		return false;
	}
	
	public static boolean isSendClose(Context context) {
		try {
			final SharedPreferences pref = PreferenceManager
			.getDefaultSharedPreferences(context);
			return pref.getBoolean(SENDCLOSE_TOGGLE, true);
		} catch (Exception e) {
		}
		return true;
	}
	
	public static boolean isShowHeaders(Context context) {
		try {
			final SharedPreferences pref = PreferenceManager
			.getDefaultSharedPreferences(context);
			return pref.getBoolean(SHOWHEADER_TOGGLE, true);
		} catch (Exception e) {
		}
		return true;
	}
	
	public static boolean isShowBody(Context context) {
		final boolean defvalue = false;
		try {
			final SharedPreferences pref = PreferenceManager
			.getDefaultSharedPreferences(context);
			return pref.getBoolean(SHOWBODY_TOGGLE, defvalue);
		} catch (Exception e) {
		}
		return defvalue;
	}
	
	public static boolean isShowBackground(Context context) {
		int sel = getBackgroundSelection(context, BG_SELECT_NO_BACKGROUND);
		return (sel != BG_SELECT_NO_BACKGROUND);
		
//		final boolean defvalue = true;
//		try {
//			final SharedPreferences pref = PreferenceManager
//			.getDefaultSharedPreferences(context);
//			return pref.getBoolean(BG_TOGGLE, defvalue);
//		} catch (Exception e) {
//		}
//		return defvalue;
	}
	
	public static boolean isAutoStart(Context context) {
		try {
			final SharedPreferences pref = PreferenceManager
			.getDefaultSharedPreferences(context);
			return pref.getBoolean(AUTOSTART_TOGGLE, false);
		} catch (Exception e) {
		}
		return false;
	}

	/**
	 * Headers in the order of to, subject
	 * @param context
	 * @return
	 */
	public static boolean[] getShowHeaders(Context context) {
		boolean[] head = new boolean[] {true, true};
		try {
			final SharedPreferences pref = PreferenceManager
			.getDefaultSharedPreferences(context);
			head[0] = pref.getBoolean(SHOWHEADER_TO_TOGGLE, true);
			head[1] = pref.getBoolean(SHOWHEADER_SUBJECT_TOGGLE, true);
		} catch (Exception e) {
		}
		return head;
	}
	
	public static String getGreetingBody(Context context, String def) {
		try {
			final SharedPreferences pref = context.getSharedPreferences(
					GREETING, Activity.MODE_PRIVATE);
			return pref.getString(NOTE_BODY, def);
		} catch (Exception e) {
		}
		return def;
	}
	
	public static void putGreetingBody(Context context, String body) {
		final SharedPreferences pref = context.getSharedPreferences(GREETING,
				Activity.MODE_PRIVATE);
		pref.edit().putString(NOTE_BODY, body).commit();
	}
	
	public static String getGreetingSubject(Context context, String def) {
		try {
			final SharedPreferences pref = context.getSharedPreferences(
					GREETING, Activity.MODE_PRIVATE);
			return pref.getString(NOTE_SUBJECT, def);
		} catch (Exception e) {
		}
		return def;
	}
	
	public static void putGreetingSubject(Context context, String subj) {
		final SharedPreferences pref = context.getSharedPreferences(GREETING,
				Activity.MODE_PRIVATE);
		pref.edit().putString(NOTE_SUBJECT, subj).commit();
	}
	
	public static String getBackgroundImagePath(Context context, String def) {
		try {
			final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
			return pref.getString(BG_PICK_PATH, def);
		} catch (Exception e) {
		}
		return def;
	}

	public static void putBackgroundImagePath(Context context, String path) {
		final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
		pref.edit().putString(BG_PICK_PATH, path).commit();
	}
	
	public static int getBackgroundSelection(Context context, int def) {
		try {
			final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
			return pref.getInt(BG_SELECT, def);
		} catch (Exception e) {
		}
		return def;
	}
	
	public static void putBackgroundSelection(Context context, int sel) {
		final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
		pref.edit().putInt(BG_SELECT, sel).commit();
	}

	public static int getBackgroundSelectedResource(int sel, int defval) {
		switch(sel) {
		case BG_SELECT_SAMPLE_ANDROID:
			return R.drawable.bg_android;
		case BG_SELECT_SAMPLE_BLUESKEY:
			return R.drawable.bg_bluishsky;
		case BG_SELECT_SAMPLE_GOODMORNING:
			return R.drawable.bg_goodmorning;
		case BG_SELECT_NO_BACKGROUND:
			return R.drawable.background_black;
		default:
			return defval;
		}
	}

	/**
	 * @param context
	 * @return nickname or email if name is not entered
	 */
	public static String getSender(Context context) {
		final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
		String n = p.getString(NICKNAME, null);
		if (TextUtils.isEmpty(n)) {
			n = p.getString(EMAIL, null);
		}
		return n;
	}
	
	public static String getEtalkFolder(Context context, String defval) {
		try {
			final SharedPreferences pref = PreferenceManager
			.getDefaultSharedPreferences(context);
			return pref.getString(ETALK_FOLDER, defval);
		} catch (Exception e) {
		}
		return defval;
	}

	/**
	 * Audio format MIME type
	 * @param context
	 * @return
	 */
	public static String getAudiotFormat(Context context) {
		String defval = FileFormats.AUDIO_3GPP;
		try {
			final SharedPreferences pref = PreferenceManager
			.getDefaultSharedPreferences(context);
			return pref.getString(AUDIO_FORMAT, defval);
		} catch (Exception e) {
		}
		return defval;
	}

	public static long getAudioMax(Context context) {
		long defval = Constants.AUDIO_FILE_MAX_SIZE;
		try {
			final SharedPreferences pref = PreferenceManager
			.getDefaultSharedPreferences(context);
			String s = pref.getString(AUDIO_MAXSIZE, null);
			if (s == null) {
				return defval;
			}
			long kb = Long.parseLong(s);
			if (kb > 0) {
				return Math.min((kb * 1024L), defval);
			} else {
				return defval;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return defval;
	}
	
	public static int getAudioSampleRate(Context context) {
		int defval = AUDIO_SAMPLE_RATE_DEFAULT;
		try {
			final SharedPreferences pref = PreferenceManager
			.getDefaultSharedPreferences(context);
			String s = pref.getString(AUDIO_SAMPLE_RATE, null);
			if (s == null) {
				return defval;
			}
			return Integer.parseInt(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return defval;
	}

	public static void putAudiotFormat(Context context,
			String mime) {
		final SharedPreferences pref = PreferenceManager
		.getDefaultSharedPreferences(context);
		pref.edit().putString(AUDIO_FORMAT, mime).commit();
	}
}
